package me.zhengjie.modules.dcompany.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.modules.dcompany.domain.DependentCompany;
import me.zhengjie.modules.dcompany.service.DependentCompanyService;
import me.zhengjie.modules.dcompany.service.dto.DependentCompanyDto;
import me.zhengjie.modules.dcompany.service.dto.DependentCompanyQueryParam;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.dcompany.service.mapper.DependentCompanyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-23
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DependentCompanyService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DependentCompanyServiceImpl extends CommonServiceImpl<DependentCompanyMapper, DependentCompany> implements DependentCompanyService {

    // private final RedisUtils redisUtils;
    private final DependentCompanyMapper dependentCompanyMapper;

    @Override
    public PageInfo<DependentCompanyDto> queryAll(DependentCompanyQueryParam query, Pageable pageable) {
        IPage<DependentCompany> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DependentCompany> page = dependentCompanyMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DependentCompanyDto.class);
    }

    @Override
    public List<DependentCompanyDto> queryAll(DependentCompanyQueryParam query){
        return ConvertUtil.convertList(dependentCompanyMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DependentCompanyDto.class);
    }

    @Override
    public DependentCompany getById(Long id) {
        return dependentCompanyMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DependentCompanyDto findById(Long id) {
        return ConvertUtil.convert(getById(id), DependentCompanyDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DependentCompanyDto resources) {
        long time = new Date().getTime();
        resources.setDcompanyId(time);
        DependentCompany entity = ConvertUtil.convert(resources, DependentCompany.class);
        return dependentCompanyMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DependentCompanyDto resources){
        DependentCompany entity = ConvertUtil.convert(resources, DependentCompany.class);
        int ret = dependentCompanyMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return dependentCompanyMapper.deleteBatchIds(ids);
    }

    @Override
    public List<DependentCompany> getDcompanyById(String companyId) {
        QueryWrapper<DependentCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        List<DependentCompany> dependentCompanies = baseMapper.selectList(queryWrapper);
        return dependentCompanies;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DependentCompanyDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DependentCompanyDto dependentCompany : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("关联总公司主键id", dependentCompany.getCompanyId());
              map.put("分公司名称", dependentCompany.getDcompanyName());
              map.put("分公司地址", dependentCompany.getDcompanyAddress());
              map.put("分公司属地编码", dependentCompany.getDcompanyCode());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
